package Demo;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

public class Demo89 {
    public static void main(String[] args) throws Exception {
        JFrame jf = new JFrame();
        jf.setBackground(Color.WHITE);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setBounds(200, 200, 500, 500);
        //************************
        Container con = jf.getContentPane();
        con.setLayout(new BorderLayout());
        //*******************************
        Image[] image=new Image[]{
                ImageIO.read(new File("D:\\test\\images\\1.jpg")),
                ImageIO.read(new File("D:\\test\\images\\2.jpg")),
                ImageIO.read(new File("D:\\test\\images\\3.jpg")),
                ImageIO.read(new File("D:\\test\\images\\4.jpg"))
        };
        //*******************************

        //*******************************
        JPanel top = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 0));
        top.setBackground(Color.WHITE);
        Mjpanel[] jpanel=new Mjpanel[4];
        for (int i = 0; i <4 ; i++) {
            jpanel[i]=new Mjpanel(image[i]);
            top.add(jpanel[i]);
        }
        //*****************************
        CardLayout cardLayout = new CardLayout();
        JPanel down = new JPanel();
        down.setLayout(cardLayout);
        JPanel d=new JPanel();
        d.setBackground(Color.WHITE);
        down.add(d, "null");
        JPanel[] dd=new JPanel[4];
        for (int i = 0; i <4 ; i++) {
            dd[i]=new Mjpanel(image[i]);
            down.add(dd[i],""+i);
        }
        //****************************
        for (int i = 0; i <4 ; i++) {
            int c=i;
            jpanel[i].addMouseListener(new MouseAdapter() {
                @Override
                public void mouseEntered(MouseEvent e) {
                    cardLayout.show(down,""+c);
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    cardLayout.show(down,"null");
                }
            });

        }
        //****************************
        con.add(top,BorderLayout.NORTH);
        con.add(down,BorderLayout.CENTER);
        jf.setVisible(true);
    }
}

@SuppressWarnings("all")
class Mjpanel extends JPanel {
    private Image image;

    public Mjpanel(Image image) {
        this.image = image;
        this.setBorder(new EmptyBorder(100, 0, 0, 0));
        this.setBorder(new LineBorder(Color.white, 10));
        setPreferredSize(new Dimension(100, 100));

    }

    @Override
    protected void paintComponent(Graphics g) {
        g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null);
    }
}
